package com.example.demo.api;

/**
 * @author Shanks
 * @date 2020-03-28
 */
public enum ApiErrorCode implements ErrorCode {

    SUCCESS(0, "操作成功"),
    ERROR(-1, "操作失败");

    private final int code;
    private final String message;

    public static ApiErrorCode fromCode(int code) {
        ApiErrorCode[] arr = ApiErrorCode.values();
        for (ApiErrorCode e : arr) {
            if (e.getCode() == code) {
                return e;
            }
        }
        return SUCCESS;
    }

    ApiErrorCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    @Override
    public String toString() {
        return String.format("ApiErrorCode{code=%d, message=%s}", code, message);
    }
}